package com.itheima.dao;

import com.itheima.bean.Course;
import com.itheima.bean.Student;

import java.io.*;
import java.util.ArrayList;

public class SutdentDao {
    private static ArrayList<Course> list1 = new ArrayList<>();
    public ArrayList<Student> readstudent() throws IOException {
        ArrayList<Student> list = new ArrayList<>();
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\student.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            int id1 = Integer.parseInt(id);
            String password = split[1];
            Student student = new Student(id1, password);
            list.add(student);
        }
        bf.close();
        return list;
    }

    public ArrayList<Course> findId() throws IOException {
        ArrayList<Course> tab = new ArrayList<>();
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\course.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String type = split[2];
            Course course = new Course(id, name, type);
            tab.add(course);
        }

        return tab;
    }

    public boolean addcourse(Course course) throws IOException {
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\course.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String type = split[2];
            Course course1 = new Course(id, name, type);
            list1.add(course1);
        }
        list1.add(course);
        BufferedWriter bu = new BufferedWriter(new FileWriter("menjinlin\\course1.txt",true));
        //for (Course course1 : list1) {
            bu.write(course.getCid() + "," + course.getCname() + "," + course.getCtype() + "\t\r");
            bu.flush();


        return true;
    }

    public void deletecourse(String id) throws IOException {
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\course1.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id1 = split[0];
            String name = split[1];
            String type = split[2];
            Course course1 = new Course(id1, name, type);
            list1.add(course1);
            for (int i = 0; i < list1.size(); i++) {
                Course course = list1.get(i);
                if (id.equals(course.getCid())) {
                    list1.remove(i);
                }
            }
        }
        BufferedWriter bu = new BufferedWriter(new FileWriter("menjinlin\\course1.txt"));
        for (Course course1 : list1) {
            bu.write(course1.getCid() + "," + course1.getCname() + "," + course1.getCtype() + "\t\r");
            bu.flush();
        }
    }

    public void findcourse() throws IOException {
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\course1.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id1 = split[0];
            String name = split[1];
            String type = split[2];
            System.out.println(id1 + "\t" + name + "\t" + type);

        }
    }
}
